#!/usr/bin/perl
use strict;
use modules::encode;
use Digest::MD5 qw(md5_base64);

sub nsunregister {
    my ($dbh,$remote,$nsnum,$input) = @_;
    my $sth = $dbh->prepare("SELECT auth FROM ninfo WHERE num = '$$input[0]' AND code = '".encode::encode($$input[0])."';");
    $sth->execute;
    my $auth = $sth->fetchrow_array;
    if ($$input[4] eq "") {
	print $remote "$nsnum O $$input[0] :Syntax: UNREGISTER <password>\n";
    }
    elsif (!auth) {
	print $remote "$nsnum O $$input[0] :You have to be identified with me before using this command.\n";
    }
    else {
	$sth=$dbh->prepare("SELECT password FROM nicknames WHERE nick = '$auth';");
	$sth->execute;
	my $pass = $sth->fetchrow_array;
	if ($pass ne md5_base64($$input[4])) {
	    print $remote "$nsnum O $$input[0] :Incorrect Password.\n";
	}
	else {
	    print $remote "$nsnum O $$input[0] :Unregistering your nick..\n";
	    $sth=$dbh->prepare("DELETE FROM channels WHERE owner = '$auth';");
	    $sth->execute;
	    $sth=$dbh->prepare("DELETE FROM users WHERE rnick = '$auth';");
	    $sth->execute;
	    $sth=$dbh->prepare("DELETE FROM nicknames WHERE rnick = '$atuj';");
	    $sth->execute;
	    $sth=$dbh->prepare("UPDATE ninfo SET auth = '0' WHERE code = '".encode::encode($$input[0])."' AND num = '$$input[0]';");
	    $sth->execute;
	    print $remote "$nsnum O $$input[0] :Your records have been \002Deleted!\002\n";
	}
    }
}
1;
